#include "../Base/stdafx.h"
#include "../Base/GameEngine.h"
#include "../Base/DxDevice.h"
#include "mysql.h"
#include "MyConnect.h"
#include "MD5.h"
#include <boost/foreach.hpp>
#include "../Base/Functions.h"

MySql::MySql()
{

}

MySql::~MySql()
{

}

void MySql::Update(const tstring &table, const tstring& where, const map<tstring,tstring>& values)
{
	tstring queText = _T("UPDATE ") + table + _T(" SET ");
	pair<tstring, tstring> myPair;
	BOOST_FOREACH(myPair, values) { queText += myPair.first + _T(" = '") + myPair.second + _T("', "); }
	queText = queText.substr(0, queText.size()-2);
	queText += _T(" WHERE ") + where;
	ExecuteQuery(queText);
}

void MySql::Update(const tstring& table, const tstring& where,  const tstring &name, const tstring &value)
{
	tstring queText = _T("UPDATE ") + table + _T(" SET ") + name + _T(" = '") + value + _T("' WHERE ") + where;
	ExecuteQuery(queText);
}

void MySql::ExecuteQuery(const tstring& queryText)
{
	MyConnect connection = MyConnect();
	if(connection.GetConnection())
	{
		mysqlpp::Query query = connection.GetConnection().query();
		string que(queryText.begin(), queryText.end());
		query << que;
		query.execute();
		//mysqlpp::StoreQueryResult res = query.store();
	}
}

const tstring MySql::GetTime() const
{
	SYSTEMTIME time;
	GetSystemTime(&time);
	tstringstream buffer;
	buffer << time.wYear;
	if(time.wMonth < 10) buffer << _T("0");
	buffer << time.wMonth;
	if(time.wDay < 10) buffer << _T("0");
	buffer << time.wDay;
	if(time.wHour < 10) buffer << _T("0");
	buffer << time.wHour;
	if(time.wMinute < 10) buffer << _T("0");
	buffer << time.wMinute;
	if(time.wSecond < 10) buffer << _T("0");
	buffer << time.wSecond;
	return buffer.str();
}

const tstring MySql::Get(const tstring& table, const tstring& name, const tstring& where)
{
	MyConnect connection = MyConnect();
	tstring que = _T("SELECT ") + name + _T(" FROM ") + table + _T(" WHERE ") + where;
	string que2(que.begin(), que.end());
	mysqlpp::Query query = connection.GetConnection().query();
	query << que2;

	mysqlpp::StoreQueryResult res = query.store();
	if(res && res.num_rows() > 0)
	{
		mysqlpp::Row row = *(res.begin());
		string out = row[0];
		return tstring(out.begin(), out.end());
	}
	return _T("");
}

const map<tstring, tstring> MySql::Get(const tstring& table, const tstring& where)
{
	MyConnect connection = MyConnect();
	tstring que = _T("SELECT * FROM ") + table + _T(" WHERE ") + where;
	string que2(que.begin(), que.end());
	mysqlpp::Query query = connection.GetConnection().query();
	query << que2;

	map<tstring, tstring> output;
	mysqlpp::StoreQueryResult res = query.store();
	if(res && res.num_rows() > 0)
	{
		mysqlpp::Row row = *(res.begin());
		for(int t = 0; t < (int)res.num_fields(); ++t)
		{
			tstring name(res.field_name(t).begin(), res.field_name(t).end());
			tstring value(row[t].begin(), row[t].end());
			output.insert(make_pair(name, value));
		}
	}
	return output;
}

void MySql::Insert(const tstring& table, const map<tstring, tstring>& values)
{
	tstring rows; tstring tvalues;
	pair<tstring, tstring> myPair;
	BOOST_FOREACH(myPair, values)
	{
		rows += myPair.first + _T(", ");
		tvalues += myPair.second + _T(", ");
	}

	tstring queText = _T("INSERT INTO ") + table + _T(" (") + rows.substr(0, rows.size()-2) + _T(") VALUES (") +  tvalues.substr(0, tvalues.size()-2) + _T(")");
	ExecuteQuery(queText);
}

void MySql::DeleteRow(const tstring &table, int id)
{
	tstring que = _T("DELETE FROM ") + table + _T(" WHERE id='") + MakeString(id) + _T("'");
	ExecuteQuery(que);
}